//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Mapping.Properties;

namespace EntLibContrib.Mapping.Configuration.Design
{
	/// <summary>
	/// Represents a mapping using an Xml data file.
	/// </summary>
	public sealed class BasicMapperNode : MapperNode
	{
		#region Fields
		private string domainObjectTypeName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the Domain Object <see cref="Type"/> that implements <see cref="Object"/>.
		/// </summary>
		/// <value>
		/// The <see cref="Type"/> that implements <see cref="Object"/>.
		/// </value>
		[Required]
		[Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(Object))]
		[MappingCategory("CategoryProvider")]
		[MappingDescription("BasicMapperDomainObjectTypeNameDescription")]
		public string DomainObjectTypeName
		{
			get { return domainObjectTypeName; }
			set { domainObjectTypeName = value; }
		}

		/// <summary>
		/// Gets a <see cref="MapperData"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="MapperData"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override MapperBaseData MapperData
		{
			get { return new BasicMapperData(Name, TableName, domainObjectTypeName); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="BasicMapperNode"/> class.
		/// </summary>
		public BasicMapperNode ()
			: this(new BasicMapperData(InternalResources.DefaultBasicMapperNodeName, string.Empty, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the<see cref="BasicMapperNode"/> class with a <see cref="BasicMapperData"/> configuration object.
		/// </summary>
		/// <param name="mapperData">The mapper data.</param>
		public BasicMapperNode(BasicMapperData mapperData)
		{
			if (mapperData == null)
				throw new ArgumentNullException("mapperData");

			Rename(mapperData.Name);
			this.TableName = mapperData.TableName;
			this.domainObjectTypeName = mapperData.DomainObjectTypeName;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Clones this instance.
		/// </summary>
		/// <returns></returns>
		public override MapperBaseNode Clone()
		{
			BasicMapperNode mapperNode = new BasicMapperNode((BasicMapperData)this.MapperData);
			foreach (ConfigurationNode mapNode in this.Nodes)
			{
				MapBaseNode node = mapNode as MapBaseNode;
				if (node != null)
				{
					MapBaseNode newMapNode = node.Clone();
					mapperNode.AddNode(newMapNode);
				}
			}
			return mapperNode;			
		}
		#endregion
	}
}